package com.sdry.web.controller.hpf;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sdry.model.hpf.LineSideLibraryOut;
import com.sdry.model.hpf.Page;
import com.sdry.service.hpf.LineSideLibraryOutService;
import com.sdry.utils.ResponseUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

/**
 * @ClassName: LineSideLibraryOutController 
 * @Description:线边库出库Controller
 * @Author hpf
 * @Date 2018年9月19日 上午11:44:31 
 * @version 1.0
 */
@Controller
@RequestMapping(value="/lineSideLibraryOut")
public class LineSideLibraryOutController {

	@Resource
	private LineSideLibraryOutService lineSideLibraryOutService;
	
	/**
	 * 分页查询线边库出库
	 * @param page 分页条件
	 * @param lineSideLibraryOut 查询条件
	 * @param response
	 */
	@RequestMapping(value="/listPageLineSideLibraryOut")
	public void listPageLineSideLibraryOut(Page page,LineSideLibraryOut lineSideLibraryOut,HttpServletResponse response){
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if (lineSideLibraryOut.getPaname() != null) {
			map.put("paname", lineSideLibraryOut.getPaname());
		} else {
			map.put("paname", "");
			lineSideLibraryOut.setPaname("");
		}
		List<LineSideLibraryOut> lineSideLibraryOutList = lineSideLibraryOutService.listPageLineSideLibraryOut(map);
		if(lineSideLibraryOut.getProductionOrder() == null){
			lineSideLibraryOut.setProductionOrder("");
		}
		int count = lineSideLibraryOutService.countLineSideLibraryOut(lineSideLibraryOut);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(lineSideLibraryOutList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 根据时间分页查询线边库出库
	 * @param page 分页条件
	 * @param lineSideLibraryOut 查询条件
	 * @param response
	 */
	@RequestMapping(value="/listPageLineSideLibraryOutByTime")
	public void listPageLineSideLibraryOutByTime(Page page,LineSideLibraryOut lineSideLibraryOut,HttpServletResponse response){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Map<String, Object> map = new HashMap<>();
		map.put("page", page.getPage());
		map.put("limit", page.getLimit());
		if (lineSideLibraryOut.getReserve5() != null && !"".equals(lineSideLibraryOut.getReserve5())) {
			map.put("outDate", lineSideLibraryOut.getReserve5());
			try {
				lineSideLibraryOut.setOutDate(format.parse(lineSideLibraryOut.getReserve5()));
			} catch (ParseException e) {
				e.printStackTrace();
			}
		} else {
			map.put("outDate", new Date());
			lineSideLibraryOut.setOutDate(new Date());
		}
		if (lineSideLibraryOut.getReserve1() != null && !"".equals(lineSideLibraryOut.getReserve1())) {
			map.put("reserve1", lineSideLibraryOut.getReserve1());
		} else {
			map.put("reserve1", "");
			lineSideLibraryOut.setReserve1("");
		}
		List<LineSideLibraryOut> lineSideLibraryOutList = lineSideLibraryOutService.listPageLineSideLibraryOutByTime(map);
		int count = lineSideLibraryOutService.countLineSideLibraryOutByTime(lineSideLibraryOut);
		JSONObject jsonObject = new JSONObject();
		JsonConfig jsonConfig = new JsonConfig();
		JSONArray jsonArray = JSONArray.fromObject(lineSideLibraryOutList, jsonConfig);
		jsonObject.put("code", 0);
		jsonObject.put("data", jsonArray);
		jsonObject.put("count", count);
		try {
			ResponseUtil.write(response, jsonObject);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
